<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="lg" title="新建岗位" scrollable no-close-on-backdrop centered>
        <div class="d-block text-center">
            <!--begin::div-->
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed" method="post">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_departinfo_name">隶属部门</label>
                        <div class="col-md-3">
                            <input class="form-control" type="hidden" maxlength="32" readonly="readonly" v-model="postForm.xt_departinfo_id" placeholder="请选择隶属部门">
                            <div class="input-group">
                                <!-- <div class="input-group-prepend"><span class="input-group-text"><i class="flaticon-refresh"></i></span></div> -->
                                <input type="text" class="form-control" v-model="postForm.xt_departinfo_name" placeholder="请选择隶属部门" aria-describedby="basic-addon2"/>
                                <div class="input-group-append"  @click="showDepartSelectModal();"><span class="input-group-text"><i class="flaticon-user"></i></span></div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_post_parentId">上级岗位</label>
                        <div class="col-md-3">
                            <input class="form-control" type="hidden" maxlength="32" readonly="readonly" value="0" v-model="postForm.xt_post_parentId" placeholder="请选择上级岗位">
                            <div class="input-group">
                                <!-- <div class="input-group-prepend"><span class="input-group-text"><i class="flaticon-refresh"></i></span></div> -->
                                <input type="text" class="form-control" v-model="postForm.xt_post_parentName" placeholder="请选择上级岗位" aria-describedby="basic-addon2"/>
                                <div class="input-group-append"  @click="showPostSelectModal();"><span class="input-group-text"><i class="flaticon-user"></i></span></div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_post_name">岗位名称</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="50" v-model="postForm.xt_post_name" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label"  jEhcLang="p_code">编&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="20" v-model="postForm.p_code" placeholder="请输入" >
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_post_desc">描&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;述</label>
                        <div class="col-md-6">
                            <textarea class="form-control" maxlength="200" v-model="postForm.xt_post_desc" placeholder="请输入"></textarea>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_post_maxNum">最多人数</label>
                        <div class="col-md-1">
                            <el-input-number 
                            v-model="postForm.xt_post_maxNum" 
                            maxlength="10" 
                            style="width:100px;" 
                            controls-position="right"
                             @change="handleChange" 
                             :min="0" 
                             :max="9999">
                            </el-input-number>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label" jEhcLang="xt_post_grade">岗位级别</label>
                        <div class="col-md-1">
                            <el-input-number 
                            v-model="postForm.xt_post_grade" 
                            maxlength="10" 
                            style="width:100px;" 
                            controls-position="right"
                             @change="handleChange" 
                             :min="0" 
                             :max="10">
                            </el-input-number>
                        </div>
                    </div>
                </div>
                <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
                    <div class="m-form__actions m-form__actions--solid">
                        <div class="form-group m-form__group row">
                            <div class="col m--align-left">
                            </div>
                            <div class="col m--align-right">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!--end::div-->
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-primary font-weight-bold mr-2" block @click="restForm">
                <i class="icon-2x flaticon2-reply"></i> 重置
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="addXtPost">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
        <!---定义接收子组件传递值方法，采用change方式-->
        <!---定义接收子组件选择部门，采用change方式-->
        <DepartSelect ref="DepartSelectRef" @change="getSelectDepartData"></DepartSelect>
        <!---定义接收子组件选择岗位，采用change方式-->
        <PostSelect ref="PostSelectRef" @change="getSelectPostData"></PostSelect>
    </b-modal>        
</template>
<script>
import DepartSelect from "@/components/depart-select.vue";
import PostSelect from "@/components/post-select.vue";
import apiUtil from "@/core/util/apiUtil.js"; 
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
export default {
    data(){
        return {            
            pickerOptions: {
                // disabledDate(time) {//范围不可选
                //     return time.getTime() > Date.now();
                // },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, 
                {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, 
                {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
        },
        postForm:{
            xt_departinfo_id: "",
            xt_departinfo_name: "",
            xt_post_parentId:"0",
            xt_post_parentName:"无",
            xt_post_name:"",
            p_code:"",
            xt_post_desc:"",
            xt_post_maxNum:"",
            xt_post_grade:""
        }
      }
    },
    components: {
        DepartSelect,
        PostSelect
    },
    watch: {
        "postForm.xt_departinfo_id":{//监听字段变化
            handler:function(newVal,old){
                this.postForm.xt_post_parentId = "";
                this.postForm.xt_post_parentName = "";                
                if(newVal == "" || newVal == undefined || newVal == null){
                }else{
                    this.$refs.PostSelectRef.initPostDataList(newVal);
                }
            }
        },
    },
    mounted() {
        
    },
    methods: {
     handleChange(value) {
      console.log(value);
     },
     addXtPost(){
        console.log("postForm",this.postForm); 
        if(this.postForm.xt_departinfo_name === ""){
          handleAlert("请选择部门", "warning", 3)
          return;
        }
        if(this.postForm.xt_post_name === ""){
          handleAlert("请名称", "warning", 3)
          return;
        }
        if(this.postForm.p_code === ""){
          handleAlert("请输入编码", "warning", 3)
          return;
        }
        // 保存前提示
        this.$confirm("确定保存?", "提示", {type: "warning",}).then(() => {
            apiUtil.post(process.env.VUE_APP_SYS_API+"/xtPost/add", this.postForm).then(({ data }) => {
                if(data.success){
                    handleAlert("创建岗位成功", "success", 3);
                    this.hideModal();//关闭窗体
                    this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                }else {
                    handleAlert("创建岗位失败", "error", 3);
                }            
            });
        }).catch(()=>{});//注意这里，这里是重点！！！;   
     },
     showModal() {
       this.restForm();
       this.$refs['my-modal'].show();
     },
     hideModal() {
       this.$refs['my-modal'].hide()
     },
     toggleModal() {
       // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
       this.$refs['my-modal'].toggle('#toggle-btn')
     },
     showDepartSelectModal(){//上级部门选择器
        this.$refs.DepartSelectRef.showModal()
     },
     showPostSelectModal(){//岗位选择器
        this.$refs.PostSelectRef.showModal()
     },
     restForm(){
        //重置
        Object.assign(this.$data.postForm, this.$options.data().postForm);
     },
     getSelectDepartData(data){//接收子组件选中部门值
        this.postForm.xt_departinfo_id = data.id;
        this.postForm.xt_departinfo_name = data.name;
     },
     getSelectPostData(data){//接收子组件选中岗位值
        this.postForm.xt_post_parentId = data.id;
        this.postForm.xt_post_parentName = data.name;
     },
   }
}
</script>

<style>
</style>